package com.hepinghospital.user.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.ui.activity.home.SearchDoctorActivity;
import com.hepinghospital.user.util.GlideUtil;
import com.rest.response.HomeDeptResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jz.joyoung.robot.util.UtilString;


/**
 * @time 2020/11/16 17:22
 */

public class HomeDeptAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<HomeDeptResponse> list;
    int Position;


    public HomeDeptAdapter(Context context, List<HomeDeptResponse> list) {
        this.mContext = context;
        this.list = list;
    }

    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_home_dept, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder myHolder = (ViewHolder) holder;
        if (!UtilString.isEmpty(list.get(position).url)) {
            GlideUtil.loadRoundImage(mContext, list.get(position).url, myHolder.ivDeptBg, R.drawable.ic_find_dept);
        } else {
            myHolder.ivDeptBg.setImageResource(R.drawable.ic_find_dept);
        }

        myHolder.tvDeptName.setText(list.get(position).name);

        myHolder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, SearchDoctorActivity.class);
            intent.putExtra("deptId", list.get(position).id);
            intent.putExtra("deptName", list.get(position).name);
            intent.putExtra("type", 0);
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_dept_bg)
        ImageView ivDeptBg;
        @BindView(R.id.tv_dept_name)
        TextView tvDeptName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
